package com.android.videotest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.android.htdvideoview.IjkVideoView;
import com.android.videotest.R;
import com.sansecy.android.simplevideo.Player;

public class IjkPlayerActivity extends AppCompatActivity {
    public static void start(Context context, String title) {
        Intent starter = new Intent(context, IjkPlayerActivity.class);
        starter.putExtra("title", title);
        context.startActivity(starter);
    }

    String url_2 = "http://mgcdn.vod.migucloud.com/vi1/352.0qvEdy5kldsFhtCh21HUba.1.3sih7P.mp4";//冬瓜汤
    String url_1 = "http://mgcdn.vod.migucloud.com/vi1/352.3pJTeObLRfdqIPI2YiDm0.4.7L7Yg5.mp4";
    String url_3 = "http://mgilvbpull.migucloud.com/352/VHIPEVTC_C0_0.m3u8";
    //    String url_4 = "http://mgcdn.vod.migucloud.com/vi1/352.35zmXEtlBcTEqTmpgH32nf.4.RXMvqI.mp4";
    String url_4 = "rtsp://192.168.1.25:8554/mystream";
    //    String url_4 = "rtmp://192.168.3.67:1935/live/test";
    private IjkVideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ijk_player);

        String title = getIntent().getStringExtra("title");
        setTitle(title);
        mVideoView = findViewById(R.id.videoView);
//        String url_1 = "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=188347&resourceType=video&editionType=default&source=aliyun&playUrlType=url_oss";

//        Uri uri = Uri.parse("android.resource://com.android.videotest/" + R.raw.test_vide);
//        mVideoView.loadUri(uri);
        mVideoView.loadUrl(url_2);
        mVideoView.setLooping(true);
        mVideoView.setOnCompletionListener(new Player.OnCompletionListener() {
            @Override
            public void onCompletion() {
                Log.d(this.getClass().getSimpleName() + "-App", "onCompletion: ");
            }
        });
        mVideoView.setOnInfoListener(new Player.OnInfoListener() {
            @Override
            public boolean onInfo(int what, int extra) {
                Log.d(this.getClass().getSimpleName() + "-App", "onInfo() called with: what = [" + what + "], extra = [" + extra + "]");
                return false;
            }
        });
        mVideoView.setOnErrorListener(new Player.OnErrorListener() {
            @Override
            public boolean onError(int what, int extra) {
                Log.d(this.getClass().getSimpleName() + "-App", "onError() called with: what = [" + what + "], extra = [" + extra + "]");
                return false;
            }
        });
        mVideoView.setOnPreparedListener(new Player.OnPreparedListener() {
            @Override
            public void onPrepared() {
                int currentPosition = mVideoView.getCurrentPosition();
                Log.d(this.getClass().getSimpleName() + "-App", "onPrepared: " + currentPosition);
                int duration = mVideoView.getDuration();
                Log.d(this.getClass().getSimpleName() + "-App", "onPrepared: " + duration);

            }
        });
        mVideoView.setOnInfoListener(new Player.OnInfoListener() {
            @Override
            public boolean onInfo(int what, int extra) {
                Log.d(this.getClass().getSimpleName() + "-App", "onInfo() called with: what = [" + what + "], extra = [" + extra + "]");
                return false;
            }
        });
        mVideoView.openVideo();
        mVideoView.start();
//        mVideoView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVideoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.stopPlayback();
    }

    public void btn1(View view) {
        mVideoView.loadUrl(url_1);
        mVideoView.openVideo();
    }

    public void btn2(View view) {
        mVideoView.loadUrl(url_2);
        mVideoView.openVideo();
    }

    public void btn3(View view) {
        mVideoView.loadUrl(url_3);
        mVideoView.openVideo();

    }

    public void btn4(View view) {
        mVideoView.loadUrl(url_4);
        mVideoView.openVideo();
    }
}
